/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.property.Acl;
/*     */ import com.zimbra.cs.mailbox.Document;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.Folder.FolderOptions;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Collection
/*     */   extends MailItemResource
/*     */ {
/*     */   protected MailItem.Type view;
/*     */   protected MailItem.Type type;
/*     */   protected int mMailboxId;
/*     */   
/*     */   public Collection(DavContext ctxt, Folder f)
/*     */     throws DavException, ServiceException
/*     */   {
/*  56 */     super(ctxt, f);
/*  57 */     setCreationDate(f.getDate());
/*  58 */     setLastModifiedDate(f.getChangeDate());
/*  59 */     setProperty("displayname", f.getName());
/*  60 */     setProperty("getcontentlength", "0");
/*  61 */     this.mId = f.getId();
/*  62 */     this.view = f.getDefaultView();
/*  63 */     this.type = f.getType();
/*  64 */     addProperties(Acl.getAclProperties(this, f));
/*  65 */     this.mMailboxId = f.getMailboxId();
/*     */   }
/*     */   
/*     */   public Collection(String name, String acct) throws DavException {
/*  69 */     super(name, acct);
/*  70 */     long now = System.currentTimeMillis();
/*  71 */     setCreationDate(now);
/*  72 */     setLastModifiedDate(now);
/*  73 */     setProperty("displayname", name.substring(1));
/*  74 */     setProperty("getcontentlength", "0");
/*     */     try {
/*  76 */       addProperties(Acl.getAclProperties(this, null));
/*     */     }
/*     */     catch (ServiceException se) {}
/*     */   }
/*     */   
/*     */   public String getContentType(DavContext ctxt)
/*     */   {
/*  83 */     if (ctxt.isWebRequest())
/*  84 */       return "text/plain";
/*  85 */     return "text/xml";
/*     */   }
/*     */   
/*     */   public boolean hasContent(DavContext ctxt)
/*     */   {
/*  90 */     return true;
/*     */   }
/*     */   
/*     */   public InputStream getContent(DavContext ctxt) throws IOException, DavException
/*     */   {
/*  95 */     if (ctxt.isWebRequest())
/*  96 */       return new ByteArrayInputStream(getTextContent(ctxt).getBytes("UTF-8"));
/*  97 */     return new ByteArrayInputStream(getPropertiesAsText(ctxt).getBytes("UTF-8"));
/*     */   }
/*     */   
/*     */   public boolean isCollection()
/*     */   {
/* 102 */     return true;
/*     */   }
/*     */   
/*     */   public java.util.Collection<DavResource> getChildren(DavContext ctxt) throws DavException
/*     */   {
/* 107 */     ArrayList<DavResource> children = new ArrayList();
/*     */     try
/*     */     {
/* 110 */       ctxt.setCollectionPath(getUri());
/* 111 */       List<MailItem> items = getChildrenMailItem(ctxt);
/* 112 */       for (MailItem item : items) {
/* 113 */         DavResource rs = UrlNamespace.getResourceFromMailItem(ctxt, item);
/* 114 */         if (rs != null)
/* 115 */           children.add(rs);
/*     */       }
/*     */     } catch (ServiceException e) {
/* 118 */       ZimbraLog.dav.error("can't get children from folder: id=" + this.mId, e);
/*     */     }
/*     */     
/* 121 */     if (isRootCollection()) {
/* 122 */       children.add(new Collection("/attachments", getOwner()));
/*     */     }
/* 124 */     return children;
/*     */   }
/*     */   
/*     */   public MailItem.Type getDefaultView()
/*     */   {
/* 129 */     return this.view;
/*     */   }
/*     */   
/*     */   private List<MailItem> getChildrenMailItem(DavContext ctxt) throws DavException, ServiceException {
/* 133 */     Mailbox mbox = getMailbox(ctxt);
/*     */     
/* 135 */     List<MailItem> ret = new ArrayList();
/*     */     
/*     */ 
/* 138 */     ret.addAll(mbox.getItemList(ctxt.getOperationContext(), MailItem.Type.FOLDER, this.mId));
/* 139 */     ret.addAll(mbox.getItemList(ctxt.getOperationContext(), MailItem.Type.DOCUMENT, this.mId));
/* 140 */     ret.addAll(mbox.getItemList(ctxt.getOperationContext(), MailItem.Type.WIKI, this.mId));
/* 141 */     ret.addAll(mbox.getItemList(ctxt.getOperationContext(), MailItem.Type.CONTACT, this.mId));
/* 142 */     return ret;
/*     */   }
/*     */   
/*     */   public Collection mkCol(DavContext ctxt, String name) throws DavException {
/* 146 */     return mkCol(ctxt, name, this.view);
/*     */   }
/*     */   
/*     */   public Collection mkCol(DavContext ctxt, String name, MailItem.Type view) throws DavException {
/*     */     try {
/* 151 */       Mailbox mbox = getMailbox(ctxt);
/* 152 */       Folder.FolderOptions fopt = new Folder.FolderOptions().setDefaultView(view);
/* 153 */       Folder f = mbox.createFolder(ctxt.getOperationContext(), name, this.mId, fopt);
/* 154 */       return (Collection)UrlNamespace.getResourceFromMailItem(ctxt, f);
/*     */     } catch (ServiceException e) {
/* 156 */       if (e.getCode().equals("mail.ALREADY_EXISTS"))
/* 157 */         throw new DavException("item already exists", 409, e);
/* 158 */       if (e.getCode().equals("service.PERM_DENIED")) {
/* 159 */         throw new DavException("permission denied", 403, e);
/*     */       }
/* 161 */       throw new DavException("can't create", 500, e);
/*     */     }
/*     */   }
/*     */   
/*     */   public DavResource createItem(DavContext ctxt, String name) throws DavException, IOException
/*     */   {
/* 167 */     Mailbox mbox = null;
/*     */     try {
/* 169 */       mbox = getMailbox(ctxt);
/*     */     } catch (ServiceException e) {
/* 171 */       throw new DavException("cannot get mailbox", 500, null);
/*     */     }
/*     */     
/* 174 */     FileUploadServlet.Upload upload = ctxt.getUpload();
/* 175 */     String ctype = upload.getContentType();
/* 176 */     if ((ctype != null) && ((ctype.startsWith("text/vcard")) || (ctype.startsWith("text/x-vcard")) || (ctype.startsWith("text/directory"))))
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 182 */       return createVCard(ctxt, name);
/*     */     }
/* 184 */     String author = ctxt.getAuthAccount().getName();
/*     */     try
/*     */     {
/* 187 */       MailItem item = mbox.getItemByPath(ctxt.getOperationContext(), ctxt.getPath());
/* 188 */       if ((item.getType() != MailItem.Type.DOCUMENT) && (item.getType() != MailItem.Type.WIKI)) {
/* 189 */         throw new DavException("no DAV resource for " + item.getType(), 406, null);
/*     */       }
/* 191 */       Document doc = mbox.addDocumentRevision(ctxt.getOperationContext(), item.getId(), author, name, null, upload.getInputStream());
/* 192 */       return new Notebook(ctxt, doc);
/*     */     } catch (ServiceException e) {
/* 194 */       if (!(e instanceof MailServiceException.NoSuchItemException)) {
/* 195 */         throw new DavException("cannot get item ", 500, null);
/*     */       }
/*     */       
/*     */       try
/*     */       {
/* 200 */         Document doc = mbox.createDocument(ctxt.getOperationContext(), this.mId, name, ctype, author, null, upload.getInputStream());
/* 201 */         Notebook notebook = new Notebook(ctxt, doc);
/* 202 */         notebook.mNewlyCreated = true;
/* 203 */         return notebook;
/*     */       } catch (ServiceException se) {
/* 205 */         throw new DavException("cannot create ", 500, se);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/* 210 */   protected DavResource createVCard(DavContext ctxt, String name) throws DavException, IOException { return AddressObject.create(ctxt, name, this); }
/*     */   
/*     */   public void delete(DavContext ctxt)
/*     */     throws DavException
/*     */   {
/* 215 */     String user = ctxt.getUser();
/* 216 */     String path = ctxt.getPath();
/* 217 */     if ((user == null) || (path == null))
/* 218 */       throw new DavException("invalid uri", 404, null);
/*     */     try {
/* 220 */       Mailbox mbox = getMailbox(ctxt);
/* 221 */       mbox.delete(ctxt.getOperationContext(), this.mId, this.type);
/*     */     } catch (ServiceException e) {
/* 223 */       throw new DavException("cannot get item", 404, e);
/*     */     }
/*     */   }
/*     */   
/*     */   protected boolean isRootCollection() {
/* 228 */     return this.mId == 1;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/Collection.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */